package com.hengxinguotong.hxgtwygsp.utils;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * TODO 通用工具类
 *
 * @author hjz
 * @version V1.0.0
 * @since 2018/12/26 13:18
 */
public class CommUtil
{

//    private static final String APPNAME = "HXGTGSP";// app名称
    private static final String CHARSET = "UTF-8";// 字符编码

    /**
     * 获取SD卡目录
     *
     * @return java.lang.String
     * @author hjz
     * @since 2019/3/25 11:12
     */
    public static String getSDDir()
    {
        String path = null;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ||
                Environment.isExternalStorageRemovable())
        {
            path = Environment.getExternalStorageDirectory().getAbsolutePath();
        }
        return path;
    }

    /**
     * 获取SD卡目录
     *
     * @return java.lang.String
     * @author hjz
     * @since 2018/12/26 13:15
     */
    public static String getSDDir(String dir)
    {
        String path = null;
        String basePath = getSDDir();
        if (basePath != null)
        {
            File file = new File(basePath+ File.separator + dir);
            if (!file.exists())
            {
                file.mkdirs();
            }
            path = file.getAbsolutePath();
        }
        return path;
    }

    /**
     * 获得手机文件目录
     *
     * @param context
     * @return java.lang.String
     * @author hjz
     * @since 2019/1/9 10:05
     */
    public static String getDataDir(Context context)
    {
        File dataDir;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ||
                Environment.isExternalStorageRemovable())
        {
            dataDir = context.getExternalFilesDir(null);
        } else
        {
            dataDir = context.getFilesDir();
        }
        return dataDir.getPath();
    }

    /**
     * 获得手机文件目录
     *
     * @param context
     * @param dir
     * @return java.io.File
     * @author hjz
     * @since 2018/12/26 13:16
     */
    public static File getDataDir(Context context, String dir)
    {
        String fileSeparator = System.getProperty("FILE_SEPARATOR", "/");
        File fileDir = new File(getDataDir(context) + fileSeparator + dir + fileSeparator);
        if (!fileDir.exists())
        {
            fileDir.mkdirs();
        }
        return fileDir;
    }

    /**
     * 获取手机缓存目录
     *
     * @param context
     * @return java.lang.String
     * @author hjz
     * @since 2019/1/9 10:05
     */
    public static String getCacheDir(Context context)
    {
        File cacheDir = null;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ||
                Environment.isExternalStorageRemovable())
        {
            cacheDir = context.getExternalCacheDir();
        }
        if (cacheDir == null)
        {
            cacheDir = context.getCacheDir();
        }
        return cacheDir.getPath();
    }

    /**
     * 验证字符串是否为空
     *
     * @param content
     * @return boolean
     * @author hjz
     * @since 2018/12/26 13:16
     */
    public static boolean isEmpty(String content)
    {
        if (content == null || "".equals(content))
        {
            return true;
        }
        return false;
    }

    /**
     * 验证网络是否可用
     *
     * @param context
     * @return boolean
     * @author hjz
     * @since 2018/12/26 13:16
     */
    public static boolean checkNet(Context context)
    {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        if (info != null && info.isConnected())
        {
            if (info.getState() == NetworkInfo.State.CONNECTED)
            {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断蓝牙是否可用
     *
     * @return boolean
     * @author hjz
     * @since 2018/12/26 13:16
     */
    public static boolean checkBluetooth()
    {
        BluetoothAdapter blueadapter = BluetoothAdapter.getDefaultAdapter();
        if (blueadapter != null && blueadapter.isEnabled())
        {
            return true;
        }
        return false;
    }

    /**
     * 验证是否是手机号
     *
     * @param mobiles
     * @return boolean
     * @author hjz
     * @since 2018/12/26 13:16
     */
    public static boolean isMobileNO(String mobiles)
    {
        if (TextUtils.isEmpty(mobiles))
        {
            return false;
        }
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9])|(17[0-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    /**
     * 验证邮箱是否合法
     *
     * @param email
     * @return boolean
     * @author hjz
     * @since 2018/12/26 13:17
     */
    public static boolean isEmailNO(String email)
    {
        if (TextUtils.isEmpty(email))
        {
            return false;
        }
        String check = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(email);
        return matcher.matches();
    }

    /**
     * 获取APP版本号
     *
     * @param context
     * @return int
     * @author hjz
     * @since 2018/12/26 13:17
     */
    public static int getAPPVersionCode(Context context)
    {
        int versionCode = 0;
        try
        {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            versionCode = info.versionCode;
        } catch (NameNotFoundException e)
        {
            e.printStackTrace();
        }
        return versionCode;
    }

    /**
     * 获取APP版本名称
     *
     * @param context
     * @return java.lang.String
     * @author hjz
     * @since 2018/12/26 13:17
     */
    public static String getAPPVersionName(Context context)
    {
        String versionName = "";
        try
        {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            versionName = info.versionName;
        } catch (NameNotFoundException e)
        {
            e.printStackTrace();
        }
        return versionName;
    }

    /**
     * 获取设备ID
     *
     * @param context
     * @return java.lang.String
     * @author hjz
     * @since 2018/12/26 13:17
     */
    public static String getDeviceId(Context context)
    {
        String deviceId = null;
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        deviceId = tm.getDeviceId();
        return deviceId;
    }

}
